import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import { enterPage } from 'utils/track';
import docs from 'utils/docs';
import * as actions from '../actions/index';
import { updateData as updateProjectData } from 'pages/index/pages/project/actions';
import BaseForm from './baseForm';
import intl from '../../../../../../../../../locale';
import { getFeature } from 'utils/features';
const { pathResource } = window.FORCE;
const cardForms = [
  {
    title: intl.get({
      id: 'code-assets.services.containers.root.CodeQuality',
      defaultMessage: '代码质量',
    }),
    id: 'p3c',
    detectionTypes: [
      {
        title: intl.get({
          id: 'code-assets.services.containers.root.JavaDevelopmentProtocolDetection',
          defaultMessage: 'Java开发规约检测',
        }),
        key: 'coding_guidelines_detection',
        desc: intl.get({
          id: 'code-assets.services.containers.root.SourceCodeComplianceScanContinuously',
          defaultMessage: '源码合规性扫描，持续提升企业代码质量，为业务稳定性保驾护航。',
        }),
        introMore: docs('scanJavaCodeSpecifications'),
        plugin: 'P3C',
      },

      {
        title: intl.get({
          id: 'code-assets.services.containers.root.IntelligentCodePatchRecommendation',
          defaultMessage: '代码补丁智能推荐',
        }),
        key: 'precfix_detection',
        desc: intl.get({
          id: 'code-assets.services.containers.root.TheIntelligentCodePatchRecommendation',
          defaultMessage:
            '基于最佳实践样本训练的智能代码补丁推荐算法，为开发者提供缺陷补丁建议，提升代码质量。',
        }),
        introMore: docs('recommendedCodePatches'),
        plugin: 'P3C',
      },

      // {
      //   title: '重复度检测',
      //   key: 'repeatability_detection',
      // },
      // {
      //   title: '复杂度检测',
      //   key: 'complexity_detection',
      // },
    ],
  },

  {
    title: intl.get({
      id: 'code-assets.services.containers.root.CodeSecurity',
      defaultMessage: '代码安全',
    }),
    id: 'security',
    detectionTypes: [
      {
        title: intl.get({
          id: 'code-assets.services.containers.root.SensitiveInformationDetection',
          defaultMessage: '敏感信息检测',
        }),
        key: 'sensitive_info_detection',
        desc: intl.get({
          id: 'code-assets.services.containers.root.ItHasIndustryLeadingSensitive',
          defaultMessage: '具备业界领先的敏感信息识别准确度，帮助企业管控信息安全。',
        }),
        introMore: docs('sensitiveInformationDetection'),
        plugin: 'SECURITY',
      },

      {
        title: intl.get({
          id: 'code-assets.services.containers.root.DependencyPackageVulnerabilityDetection',
          defaultMessage: '依赖包漏洞检测',
        }),
        key: 'dependency_analysis',
        desc: intl.get({
          id: 'code-assets.services.containers.root.BasedOnAnAuthoritativeSecurity',
          defaultMessage:
            '基于权威的安全信息库，及时识别代码依赖中存在的安全漏洞，同时提供修复方案建议。',
        }),
        introMore: docs('dependencyPackageVulnerabilityDetection'),
        plugin: 'SECURITY',
      },
    ],
  },

  {
    title: intl.get({
      id: 'code-assets.services.containers.root.InsightReport',
      defaultMessage: '洞察报表',
    }),
    id: 'insight',
    detectionTypes: [
      {
        title: '',
        key: 'project_report',
        desc: intl.get({
          id: 'code-assets.services.containers.root.ProvidesCodeLibraryDataReports',
          defaultMessage: '提供代码库数据报表，帮助企业洞察研发效能。',
        }),
        introMore: docs('insight'),
        plugin: 'project_report',
        noTrigger: true,
      },
    ],
  },
  {
    title: '文档',
    isBeta: false,
    id: 'wiki',
    detectionTypes: [
      {
        title: '',
        key: 'wiki',
        desc: '支持研发过程的文档管理，促进团队协作和知识共享。',
        noTrigger: true,
        enabled: false,
        popConfirmConfig: {
          title: '关闭后已有文档内容将保留，重新开启可继续使用，确定要关闭吗？',
        },
      },
    ],
  },
];
class Root extends Component {
  static propTypes = {
    getInfo: PropTypes.func,
  };

  static defaultProps = {
    getInfo: () => {},
  };

  componentDidMount() {
    const fullName = pathResource.fullName.split('/')[1]
      ? pathResource.fullName.split('/')[1].trim()
      : '';
    document.title = intl.get(
      {
        id: 'code-assets.services.containers.root.IntegrationAndServicesFullnameCloud',
        defaultMessage: '集成与服务 · {fullName} · 云效 Codeup',
      },
      { fullName },
    );
    this.props.getOrgServiceSetting();
    this.props.getInfo();
    enterPage('Repo-Settings-Services');
    // 滚动到指定位置
    const scrollKey = window.location.hash ? window.location.hash.slice(1) : '';
    if (scrollKey === 'lsif') {
      const container = document.getElementById('container');
      container.scrollTop = 226 + 226 + 121 + 40;
    }
  }

  componentWillUnmount() {
    this.props.resetData();
  }
  controlServicesSubItem(items) {
    const originItems = getFeature('project.setting.services.sub.items');
    if (originItems.includes('all')) {
      return items;
    } else if (originItems.length === 0) {
      return [];
    }
    const newCardForms = items.filter((item) => originItems.map((i) => i.id).includes(item.id));
    newCardForms.forEach((item) => {
      const t = originItems.find((v) => v.id === item.id);
      const keys = t.detectionTypes.map((i) => i.key);
      const tt = item.detectionTypes.filter((v) => keys.includes(v.key));
      const vv = newCardForms.find((tem) => tem.id === item.id);
      vv.detectionTypes = [];
      vv.detectionTypes.push(...tt);
    });
    return newCardForms;
  }
  render() {
    return (
      <div className="services-content">
        {this.controlServicesSubItem(cardForms).map((item) => {
          return <BaseForm cardConfig={item} {...this.props} />;
        })}
      </div>
    );
  }
}

export default connect(
  (state) => ({
    ...state.project.projectSettings.services,
    wikiEnable: state.project.root.wikiEnable,
  }),
  (dispatch) =>
    bindActionCreators(
      {
        ...actions,
        updateProjectData,
      },
      dispatch,
    ),
)(Root);
